package com.example.vaccine.controller;

import com.example.vaccine.VO.ResultVO;
import com.example.vaccine.entity.CheckRecords;
import com.example.vaccine.entity.Vaccines;
import com.example.vaccine.service.CheckService;
import com.example.vaccine.util.ResultVOUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/check")
@Tag(name = "Check Management API", description = "核查管理相关接口")
public class CheckController {

    @Autowired
    private CheckService checkService;

    @Operation(summary = "获取待核查疫苗", description = "根据核查员 ID 获取其待核查疫苗列表")
    @GetMapping("/pending/{checkerId}")
    public ResultVO<List<Vaccines>> getPendingVaccines(
            @Parameter(description = "核查员 ID", required = true) @PathVariable Integer checkerId) {
        List<Vaccines> pendingList = checkService.getPendingVaccinesByChecker(checkerId);
        return ResultVOUtil.success(pendingList);
    }

    @Operation(summary = "获取已核查记录", description = "根据核查员 ID 获取其已核查记录列表")
    @GetMapping("/checked/{checkerId}")
    public ResultVO<List<CheckRecords>> getCheckedRecords(
            @Parameter(description = "核查员 ID", required = true) @PathVariable Integer checkerId) {
        List<CheckRecords> checkedList = checkService.getCheckedRecordsByChecker(checkerId);
        return ResultVOUtil.success(checkedList);
    }

    @Operation(summary = "提交核查结果", description = "提交疫苗核查结果，包括是否通过与理由")
    @PostMapping("/submit")
    public ResultVO<?> submitCheckResult(
            @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "核查结果参数", required = true)
            @RequestBody SubmitCheckResultRequest params) {
        try {
            checkService.submitCheckResult(
                    params.getCheckerId(),
                    params.getVaccineNumbering(),
                    params.getFlag(),
                    params.getReason()
            );
            return ResultVOUtil.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVOUtil.fail();
        }
    }

    public static class SubmitCheckResultRequest {
        private Integer checkerId;
        private String vaccineNumbering;
        private Boolean flag;
        private String reason;

        public Integer getCheckerId() { return checkerId; }
        public void setCheckerId(Integer checkerId) { this.checkerId = checkerId; }

        public String getVaccineNumbering() { return vaccineNumbering; }
        public void setVaccineNumbering(String vaccineNumbering) { this.vaccineNumbering = vaccineNumbering; }

        public Boolean getFlag() { return flag; }
        public void setFlag(Boolean flag) { this.flag = flag; }

        public String getReason() { return reason; }
        public void setReason(String reason) { this.reason = reason; }
    }
}
